package hephantan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 
 * @author DIVID
 */
public class TCPEchoServer_DaLuong {

    public final static int defaultPort = 2014;

    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(defaultPort);
            System.out.println("Server dang hoat dong tai cong: " + defaultPort);
            while (true) {
                try {
                    Process p = new Process(ss.accept());
                    p.start();
                } catch (IOException ex) {
                    Logger.getLogger(TCPEchoServer_DaLuong.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(TCPEchoServer_DaLuong.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
class Process extends Thread {

    Socket s;

    public Process() {
    }

    public Process(Socket s) {
        this.s = s;
    }

    @Override
    public synchronized void start() {
        try {
//            OutputStream os = s.getOutputStream();
//            InputStream is = s.getInputStream();
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            int ch = 0;
//            while (true) {
//                ch = in.read();
//                System.out.println(ch);
//                if (ch == -1) {
//                    break;
//                }
//                out.write(ch);
//            }
            String str = in.readLine();
            str = str.substring(2, str.length()-2);
            StringTokenizer ok1 = new StringTokenizer(str, "$$");
            StringTokenizer ok2 = new StringTokenizer(ok1.nextToken().toString(), "|");
            while (ok2.hasMoreTokens()) {
                System.out.println(ok2.nextToken());
            }
            s.close();
        } catch (IOException e) {
            System.out.println("ERROR: " + e);
        }
    }
}
